import pandas as pd
import numpy as np
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline

# 1. 创建一个示例数据集
data = {
    ‘Age': [25, 30, np.nan, 35, 40, 45],
    ‘Salary': [50000, 54000, 52000, np.nan, 58000, 60000],
    ‘City': [‘Beijing', ‘Shanghai', ‘Guangzhou', ‘Beijing', ‘Shenzhen', ‘Shanghai’]
}
df = pd.DataFrame(data)
print(“原始数据：”)
print(df)

# 2. 定义预处理管道
# 2.1 分别定义数值型和类别型特征的处理器
numerical_features = [‘Age', ‘Salary’]
categorical_features = [‘City’]

# 数值型特征：用中位数填充缺失值，然后标准化
numerical_transformer = Pipeline(steps=[
    (‘imputer', SimpleImputer(strategy=‘median’)),
    (‘scaler', StandardScaler())
])

# 类别型特征：用众数填充缺失值，然后进行独热编码
categorical_transformer = Pipeline(steps=[
    (‘imputer', SimpleImputer(strategy=‘most_frequent’)),
    (‘onehot', OneHotEncoder(drop=‘first’)) # drop=‘first’ 避免共线性
])

# 2.2 使用ColumnTransformer将处理器应用到对应的列上
preprocessor = ColumnTransformer(
    transformers=[
        (‘num', numerical_transformer, numerical_features),
        (‘cat', categorical_transformer, categorical_features)
    ])

# 3. 执行预处理
df_processed = preprocessor.fit_transform(df)

# 查看处理后的数据（注意：它现在是numpy数组格式）
print(“\n预处理后的数据：”)
print(df_processed)